package com.xbongbong.paas.controller;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.message.service.MessageService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 跟进记录 及时消息推送相关接口
 * @author 魏荣杰
 * @date 2019/2/26 16:40
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/communicate")
public class MessageRabbitMqCommunicateController {
    @Resource
    private MessageService messageService;
    /**
     * Description: 跟进记录新建
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/2/27 15:38
     * @since v1.0
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_ADD_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 跟进记录点赞
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/2/27 15:38
     * @since v1.0
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String like(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_LIKE_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 跟进记录评论
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/2/27 15:38
     * @since v1.0
     */
    @RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String comment(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_COMMENT_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 跟进记录回复
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/2/27 15:38
     * @since v1.0
     */
    @RequestMapping(value = "/replay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String replay(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_REPLY_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 跟进记录@他人
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 魏荣杰
     * @date 2019/2/27 15:38
     * @since v1.0
     */
    @RequestMapping(value = "/at", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String at(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_AT_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 跟进记录评论刪除
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author 徐斌
     * @date 2019/4/17 11：03
     */
    @RequestMapping(value = "/commentDel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commentDel(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.COMMUNICATE_COMMENT_DEL_PUSH);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
